package com.zret.oasys.operation.emp;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.zret.oasys.entity.Dept;
import com.zret.oasys.entity.Emp;
import com.zret.oasys.entity.Position;
import com.zret.oasys.json.entity.MsgJson;
import com.zret.oasys.json.entity.UserInfoJson;

public class GetEmpInfo implements EmpServletOperation {

	@Override
	public void doOperation(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 设置编码
		request.setCharacterEncoding("utf-8");
		response.setContentType("application/json;charset=utf-8");
		// 输出对象的创建
		PrintWriter out = response.getWriter();
		// 创建Gson对象
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
		// 像前端传递的信息对象
		UserInfoJson userInfoJson = null;
		// 如果为空,则未登录
		if(request.getParameter("empid") == null) {
			MsgJson msg = new MsgJson("未登录", 400);
			userInfoJson = new UserInfoJson(msg);
		} else {
			// 获取用户id
			int empid = Integer.valueOf(request.getParameter("empid"));
			// 服务端传回员工信息
			Emp emp = empService.getEmpsById(empid);
			Dept dept = deptService.getDeptByDeptId(emp.getDeptid()).get(0);
			Position pos = pss.getPositionByName(emp.getPosition());
			
			// 需要向前端传递的数据
			MsgJson msg = new MsgJson("个人信息获得成功", 200);
			userInfoJson = new UserInfoJson(msg, emp, dept, pos);
		}
		// 将信息对象格式为json格式
		String jsonObj = gson.toJson(userInfoJson);
		out.print(jsonObj);
		out.close();
	}

}
